<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/24
 * Time: 14:13
 */

namespace app\statistical\controller;


use cmf\controller\AdminBaseController;
use think\Db;

class InsertClassController extends AdminBaseController
{
    public function index()
    {
        $param = $this->request->param();
        $shop_id=$this->admin_shop_id;

        $where = [
            'c.shop_id' => ['=', $this->admin_shop_id],
            'c.delete_time' => ['eq', 0]
        ];
        if (!empty($param['course_id'])) {
            //课程名称
            $where['c.id'] =$param['course_id'] ;
        }
        if (!empty($param['school_id']) && intval($param['school_id']) != 0) {
            //校区
            $where['cs.school_id'] = intval($param['school_id']);

        }
        $homework_starttime = empty($param['homework_starttime']) ? 0 : strtotime($param['homework_starttime']);
        $homework_endtime   = empty($param['homework_endtime']) ? 0 : strtotime($param['homework_endtime']);
        if (!empty($homework_starttime) && !empty($homework_endtime)) {
            $where['ct.insert_class_time'] = [['>= time', $homework_starttime], ['<= time', $homework_endtime]];
        } else {
            if (!empty($homework_starttime)) {
                $where['ct.insert_class_time'] = ['>= time', $homework_starttime];
            }
            if (!empty($homework_endtime)) {
                $where['ct.insert_class_time'] = ['<= time', $homework_endtime];
            }
        }

        /*
          * 学校
         */
        $school_data=Db::name('school')
            ->where('shop_id', '=', $this->admin_shop_id)//多店铺
            ->where('delete_time', '=', 0)//未删除
            ->where('is_school', '=', 0)//1:地区 0:学校
            ->select();


        /*
         * 课程分类
         */
        $course_data=Db::name('course')
            ->where('shop_id', '=', $this->admin_shop_id)//多店铺
            ->where('delete_time', '=', 0)//未删除
            ->select();
        



        //插班的班级详情

        $data=Db::name('class')->alias('cs')->field('s.name sname,c.course_name cname, cs.name csname,ct.progress,
            ct.insert_class_time ctcreate_time,o.pay_amount,o.amount,
            t.grade tgrade,t.name tname')
            ->join('school s','s.id=cs.school_id')
            ->join('course c','c.id=cs.course_id')
            ->join('class_student ct','ct.class_id=cs.id and ct.course_id=c.id')
            ->join('student t','t.id=ct.student_id')
            ->join('order o','o.student_id=ct.student_id and o.class_id=cs.id')
            ->where(['cs.shop_id'=>$shop_id,'ct.student_status'=>3])
            ->where('o.order_status', ['eq', 2], ['eq', 1], 'or')
            ->where($where)
//            ->buildSql();
//        dump($data);
            ->order('ct.id')
            ->paginate(10);
        //学生插班之前是否在此报过名呢






        $this->assign('data',$data);
        $this->assign('course_data', $course_data);
        $this->assign('school_data', $school_data);
        $this->assign('param', $param);
        return $this->fetch();
    }
}